package au.com.bluelink.scjp.chap6;

import java.util.regex.*;

public class RegexTest {

	/**
	 * @param args
	 */
	public static void main(String[] args) {

		System.out.println("Testing RegexTest1...");
		RegexTest1.test();
		
		// use case
		// % java RegexTest "\d\w" "ab4 56_7ab"
		if(args.length == 2) {
		System.out.println("Testing RegexTest2...");
		RegexTest2.test(args);
		} else {
			System.out.println("PROVIDE SOME ARGUMENTS FOR TESTING RegexTest2...");
			System.out.println("e.g. \"\\d\\w\" \"ab4 56_7ab\"");
		}
	}

}

class RegexTest1 {
	public static void test() {
		Pattern p = Pattern.compile("ab"); // the expression
		Matcher m = p.matcher("abaaaba"); // the source
		boolean b = false;
		while (b = m.find()) {
			System.out.println(m.start() + " ");
		}
	}
}

class RegexTest2 {
	public static void test(String[] args) {
		Pattern p = Pattern.compile(args[0]);
		Matcher m = p.matcher(args[1]);
		boolean b = false;
		System.out.println("Pattern is " + m.pattern());
		while (b = m.find()) {
			System.out.println(m.start() + " " + m.group());
		}
	}
}